#!/usr/bin/env python
import os
import getopt
import sys
import heibertsrpg.config
import google_upload

def exit():
	print "exiting"
	print
	sys.exit()
	
def listoption():
	print "This is a list of possible options for buildy"
	print
	print "[t]est: tests the program to make sure it works"
	print "[s]ource: makes a source zip file"
	print "[i]nstaller: makes an nsis installer"
	print "[z]ip: makes a zip of the current build"
	print "[q]uit: quits the program"
	print

def postFile(file):
    print "It is possible to post this on google code."
    question=raw_input("Would you like to do this? [y]es [n]o: ")
    if question == 'n':
        return
    if os.path.exists('./google_upload.py'):
        print
        name = "Heibert RPG "+heibertsrpg.config.getVersion()
        print "the file will be called "+name
        print "Beginning google_upload.py"
        username = raw_input("Enter your username: ")
        password = raw_input("Enter your password: ")
        summary = raw_input("Enter a summary of the file: ")
        project='heibertsrpg'
        print "Defaulting to project:"+project
        print "And file:"+file
        google_upload.upload(file, project, username, password, summary)
        
def test():
	print "preparing to test program..."
	print "this defaults to the heibertrpg.py file"
	os.system("python ./src/heibertrpg.py")

def buildRelease():
	print "a release "
	
def buildUsrZip():
	print "Starting to add end-user files to zip..."
	print
	print "WARNING: this requires zip to run."
	quit=raw_input("press any key to continues or [q]uit: ")
	if quit=='q':
		exit()
	print
	#mvSrc()
	#mvDoc()
	#mvData()
        os.system('zip ./build/Heibert_RPG_'+heibertsrpg.config.getVersion()+'.zip ./heibertsrpg/* ./heibertsrpg/*.py ./heibertsrpg/*/*.py ./data/*/* ./run.py ./buildy.py ./readme.txt')
        #postFile('./build/Heibert_RPG_'+heibertsrpg.config.getVersion()+'.zip')

def buildDevZip():
	print "Starting to add source files to zip..."
	print
	print "WARNING: this requires zip to run."
	quit=raw_input("press any key to continues or [q]uit: ")
	if quit=="q":
		exit()
	print
	os.system('zip ./build/src.zip ./*')

def buildWinInst():
	print "Starting to add source files to zip..."
	print
	print "WARNING: this uses makensis, it must be installed for this to work!"
	quit=raw_input("press any key to continue or [q]uit: ")
	if quit == 'q':
		exit()
	print
	os.system('makensis ./installer/installer.nsi')
	
def makebuildFolder():
	if os.path.exists("./build"):
		print "build folder already exists"
		question=raw_input("would you like to delete it? [y]es [n]o: ")
		if question == 'y':
			os.system("rm -r ./build")
		else:
			print "then we can't move on..."
			exit()

		print
	os.system('mkdir ./build')
	
def mvSrc():
	print "moving the source now..."
	os.system('cp ./build/ ./src')
	
def mvDoc():
	print "moving the documentation now..."
	os.system('cp ./build/ ./doc')

def tryagain():
	#print
	#tryagain = raw_input("Would you like to build another? [y]es [n]o: ")
	#if tryagain.lower() == 'y':
	#	build()
	print
	build()

def mvData():
	print "moving the data: images, fonts, music..."
	os.system('cp ./build/ ./data')
    
print "HI there I'm buildy the friendly python builder."
print
if os.name == 'nt':
	print "Just going to give you a friendly warning here."
	print "This has not been tested on windows yet"
	print
	
def build():
	build = raw_input("So what would you like to make? [z]ip, [i]nstaller, [q]uit, [m]ore options: ")
	print
	build = build.lower()
	if build in ["zip", "build zip", "usr zip", "usrzip", "z", "make zip", "user zip", "userzip", "build usrzip", "uz", "usrz", "uzip"]:
		makebuildFolder()
		buildUsrZip()
	elif build in ["q"]:
		exit()
	elif build in ["m"]:
		listoption()
	elif build in ['t']:
		test()
	elif build in ["s", "devzip","developmentzip", "dz", "devz", "dzip"]:
		makebuildFolder()
		buildDevZip()
	elif build in ["i", "nsis","nsi","installer","windowsinstall","win","wininst"]:
		makebuildFolder()
		buildWinInst()
        elif build in [""]:
            wanaquit=raw_input("Would you like to exit?")
            if wanaquit.lower() not in ["no", "n"]:
                exit()
	else:
		print "Sorry, couldn't understand that."
		print
	tryagain()
		
build()

if os.name == 'nt':
	print
	quest=raw_input("So did everything work ok? [y]es [n]o: ")
	if quest in ["yes"]:
		print "wow i'm surprised"
	elif quest in ['no']:
		print "yah haha"
